package com.sumu.bubei.models.login.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sumu.bubei.common.rest.entity.ResultInfo;
import com.sumu.bubei.models.login.entity.*;
import com.sumu.bubei.models.login.entity.vo.RoleAdminVo;
import com.sumu.bubei.models.login.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sumu
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {
    @Autowired
    UserServiceImpl userService;

    @Autowired
    RoleServiceImpl roleService;

    @Autowired
    RoleAndUserRelationServiceImpl roleAndUserRelationService;

    @Autowired
    PermissionAndRoleRelationServiceImpl permissionAndRoleRelationService;

    @Autowired
    PermissionServiceImpl permissionService;

    /**
     * 获取管理员 用户的权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/getRole")
    public ResultInfo getRole(Integer id) {
        RoleAdminVo roleAdminVo = new RoleAdminVo();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("userID", id);
        roleAdminVo.setUserInfo(userService.getOne(userQueryWrapper));

        QueryWrapper<RoleAndUserRelation> roleAndUserRelationQueryWrapper = new QueryWrapper<>();
        roleAndUserRelationQueryWrapper.eq("userID",id);
        RoleAndUserRelation roleAndUserRelation = roleAndUserRelationService.getOne(roleAndUserRelationQueryWrapper);
        if(roleAndUserRelation == null) {
            return new ResultInfo().success(HttpStatus.OK.value(), "当前用户没有配置角色", roleAdminVo);
        }
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("roleID", roleAndUserRelation.getRoleID());
        Role role = roleService.getOne(roleQueryWrapper);
        roleAdminVo.setRoleID(roleAndUserRelation.getRoleID());
        roleAdminVo.setRoleName(role.getRoleName());

        QueryWrapper<PermissionAndRoleRelation> permissionAndRoleRelationQueryWrapper = new QueryWrapper<>();
        permissionAndRoleRelationQueryWrapper.eq("roleID", role.getRoleID());
        List<PermissionAndRoleRelation> list = permissionAndRoleRelationService.list(permissionAndRoleRelationQueryWrapper);
        List<String> permission = new ArrayList<>();
        list.forEach((l)-> {
            QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
            permissionQueryWrapper.eq("permissionID", l.getPermissionID());
            Permission permissionServiceOne = permissionService.getOne(permissionQueryWrapper);
            permission.add(permissionServiceOne.getPermissionCode());
        });
        roleAdminVo.setPermissions(permission);

        return new ResultInfo().success(HttpStatus.OK.value(), "查询用户角色成功", roleAdminVo);
    }
}
